package com.dsteiner.xelavos.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.ListBoxWidget;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.Widgets.TextBoxExt;
import com.dsteiner.xelavos.client.controller.SavedSearchController;
import com.dsteiner.xelavos.client.domain.SavedSearch;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.dsteiner.xelavos.client.resources.XelavosResources;
import com.google.gwt.dom.client.Style.Display;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;

public class SavedSearchWidget extends LazyWidget {

	public static class SavedSearchModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(this);

		public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName, listener);
		}
	}

	private SavedSearchController savedSearchController = new SavedSearchController();
	private final SavedSearchModel savedSearchModel;
	private final AuthenticationModel authenticationModel;

	public SavedSearchWidget(SavedSearchModel savedSearchModel, AuthenticationModel authenticationModel) {
		this.savedSearchModel = savedSearchModel;
		this.authenticationModel = authenticationModel;
		XelavosResources.INSTANCE.css().savedSearchWidget();
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		HTML nameHtml = new HTML("Nombre de la busqueda:");
		HTML frequencyHtml = new HTML("Frecuencia de mandar email:");
		HTML sendIfNoMatchesHtml = new HTML("Mandar si no hay resultados:");
		TextBoxExt nameTextBox = new TextBoxExt();
		ListBoxWidget frequencyListBox = new ListBoxWidget();
		ListBoxWidget sendIfNoMatchesListBox = new ListBoxWidget();
		ButtonExt saveButton = new ButtonExt("guardar busqueda");
		
		sendIfNoMatchesListBox.addItem(SavedSearch.EMAIL_IF_NO_MATCHES_NO);
		sendIfNoMatchesListBox.addItem(SavedSearch.EMAIL_IF_NO_MATCHES_YES);
		
		frequencyListBox.addItem(SavedSearch.FREQUENCY_WEEKLY);
		frequencyListBox.addItem(SavedSearch.FREQUENCY_DAILY);
		
		mainPanel.add(new HTML("<h3>Guardar busqueda</h3>"));
		mainPanel.add(nameHtml);
		mainPanel.add(nameTextBox);
		mainPanel.add(sendIfNoMatchesHtml);
		mainPanel.add(sendIfNoMatchesListBox);
		mainPanel.add(frequencyHtml);
		mainPanel.add(frequencyListBox);
		mainPanel.add(saveButton);
		
		savedSearchController.registerSavedSearchWidget(this);
		savedSearchController.registerAuthenticationModel(authenticationModel);
		savedSearchController.registerSavedSearchModel(savedSearchModel);
		savedSearchController.registerFrequencyListBox(frequencyListBox);
		savedSearchController.registerSendIfNoMatchesListBox(sendIfNoMatchesListBox);
		savedSearchController.registerNameTextBox(nameTextBox);
		savedSearchController.registerSaveButton(saveButton);
		
		nameHtml.addStyleName(XelavosResources.INSTANCE.css().titleHtml());
		sendIfNoMatchesHtml.addStyleName(XelavosResources.INSTANCE.css().titleHtml());
		frequencyHtml.addStyleName(XelavosResources.INSTANCE.css().titleHtml());
		
		saveButton.getElement().getStyle().setDisplay(Display.BLOCK);
		
		return mainPanel;
	}

}